<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\MemberSignLog;
use App\Constants\MiniProgramConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MemberSignLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MemberSignLog(['member']), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map));

            $grid->model()->orderByDesc('id');

            $grid->column('id');
            $grid->column('mini_type')->display(function ($miniType) use ($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('member.nickname', '会员名称');
            $grid->column('member_id');
            $grid->column('continue_day');
            $grid->column('created_at', '签到日期');


            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableActions();

            $grid->filter(function (Grid\Filter $filter) use ($map) {
                $filter->panel();
                $filter->expand();
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->equal('member_id')->width(2);
                $filter->like('member.nickname', '会员名称')->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MemberSignLog(), function (Show $show) {
            $show->field('id');
            $show->field('member_id');
            $show->field('continue_day');
            $show->field('year');
            $show->field('month');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MemberSignLog(), function (Form $form) {
            $form->display('id');
            $form->text('member_id');
            $form->text('continue_day');
            $form->text('year');
            $form->text('month');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
